package com.hmall.user.interceptor;

import com.hmall.common.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * springMVC拦截器,拦截进入处理器(每一个接受请求的controller)的所有请求
 */
@Slf4j
@Component
public class MyHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //读取头信息中的 authorization
        String authorization = request.getHeader("authorization");
        if (authorization == null) {
            log.info("当前用户未登录...");
            return false;
        } else {
            //设置当前用户id到threadlocal中
            log.info("当前登录用户id==>{}", authorization);
            BaseContext.set(authorization);
            return true;
        }
    }
}
